package vader.config;

import com.alicloud.openservices.tablestore.SyncClient;
import com.alicloud.openservices.tablestore.TunnelClient;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: yongxin@mail.thefancyapp.com
 * @program: fancy_flip
 * @description: tableStore-配置
 * @create: 2020-12-08 18:10
 **/

@Data
@Slf4j
@Configuration
@ConditionalOnProperty(prefix = "tablestore",name = "enable",havingValue = "true")
public class TableStoreConfig {
    @Value("${tablestore.endpoint}")
    private String endpoint;
    @Value("${tablestore.access-key-id}")
    private String accessKeyId;
    @Value("${tablestore.access-key-secret}")
    private String accessKeySecret;
    @Value("${tablestore.instance-name}")
    private String instanceName;

    @Bean(destroyMethod = "shutdown")
    @ConditionalOnMissingBean(SyncClient.class)
    public SyncClient syncClient() {
        return createResource();
    }

    @Bean
    public TunnelClient tunnelClient() {
        try {
            return new TunnelClient(endpoint, accessKeyId, accessKeySecret, instanceName);
        } catch (Exception e) {
            log.error("TunnelClientFailed", e);
            return null;
        }
    }

    private SyncClient createResource() {
        return new SyncClient(endpoint, accessKeyId, accessKeySecret, instanceName);
    }

}
